<?php

namespace app\logic\fp;

use app\common\model\fp\Post;
use app\common\model\fp\PostUnlocked;
use app\common\model\fp\PostUserUnlocked;
use app\logic\BaseLogic;
use think\facade\Log;

class PostUnlockedLogic extends BaseLogic
{

    /**
     * 是否已经解锁
     * @param $postId
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function isUnlocked($userId, $postId): bool
    {
        $count = PostUserUnlocked::initModel($userId)->where(['user_id' => $userId, 'post_id' => $postId])->count();
        return $count > 0;
    }

    /**
     * 根据用户关联 - 查找是否已经解锁
     * @param $userId [登录用户]
     * @param $postId [动态id]
     * @return bool
     * @throws \Throwable
     */
    public function isUnlockedByUser($userId, $postId): bool
    {
        $count = PostUserUnlocked::initModel($userId)->where(['user_id' => $userId, 'post_id' => $postId])->count();
        return $count > 0;
    }

    /**
     * 根据动态关联 - 查找是否已经解锁
     * @param $userId
     * @param $postId
     * @return bool
     * @throws \Throwable
     */
    public function isUnlockedByPost($userId, $postId): bool
    {
        $count = PostUnlocked::initModel($postId)->where(['user_id' => $userId, 'post_id' => $postId])->count();
        return $count > 0;
    }

    /**
     * 解锁
     * @param $creatorId
     * @param $postId
     * @return false|void
     */
    public function unlock($creatorId, $postId, $uid)
    {
        $postUnlockedModel = PostUnlocked::initModel($postId);
        $postUserUnlockedModel = PostUserUnlocked::initModel($uid);
        $postUnlockedModel->startTrans();
        try {
            $post = Post::initModel($creatorId)->where('id', $postId)->find();
            if (!$post) {
               throw new \runtimeException(__('Record not found'));
            }
            $createTime = time();
            $check = $postUnlockedModel->where(['user_id' => $uid, 'post_id' => $postId])->find();
            if (!$check) {
                $postUnlockedModel->insert([
                    'user_id' => $uid,
                    'post_id' => $postId,
                    'post_user_id' => $creatorId,
                    'create_time' => $createTime,
                ]);
            }
            $check = $postUserUnlockedModel->where(['user_id' => $uid, 'post_id' => $postId])->find();
            if (!$check) {
                $postUserUnlockedModel->insert([
                    'user_id' => $uid,
                    'post_id' => $postId,
                    'post_user_id' => $creatorId,
                    'create_time' => $createTime,
                ]);
            }
            $postUnlockedModel->commit();
        } catch (\Throwable $e) {
            $postUnlockedModel->rollback();
            Log::channel('my')->error($e->getMessage());
            throw $e;
        }

    }

    /**
     * 根据用户ID获取解锁的动态ID
     * @param $userId
     * @return array
     * @throws \Throwable
     */
    public function getUnlockedIdsByUid($userId)
    {
        return PostUserUnlocked::initModel($userId)->where('user_id', $userId)->column('post_id');
    }
}